home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW re2c 1.1 / examples / cmmap.re < prev    next >
Encoding:
Text File  |  1995-06-01  |  5.2 KB  |  268 lines  |  [TEXT/MPS ]

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <sys/mman.h>
  5. #include <string.h>
  6.  
  7. #define    ADDEQ    257
  8. #define    ANDAND    258
  9. #define    ANDEQ    259
  10. #define    ARRAY    260
  11. #define    ASM    261
  12. #define    AUTO    262
  13. #define    BREAK    263
  14. #define    CASE    264
  15. #define    CHAR    265
  16. #define    CONST    266
  17. #define    CONTINUE    267
  18. #define    DECR    268
  19. #define    DEFAULT    269
  20. #define    DEREF    270
  21. #define    DIVEQ    271
  22. #define    DO    272
  23. #define    DOUBLE    273
  24. #define    ELLIPSIS    274
  25. #define    ELSE    275
  26. #define    ENUM    276
  27. #define    EQL    277
  28. #define    EXTERN    278
  29. #define    FCON    279
  30. #define    FLOAT    280
  31. #define    FOR    281
  32. #define    FUNCTION    282
  33. #define    GEQ    283
  34. #define    GOTO    284
  35. #define    ICON    285
  36. #define    ID    286
  37. #define    IF    287
  38. #define    INCR    288
  39. #define    INT    289
  40. #define    LEQ    290
  41. #define    LONG    291
  42. #define    LSHIFT    292
  43. #define    LSHIFTEQ    293
  44. #define    MODEQ    294
  45. #define    MULEQ    295
  46. #define    NEQ    296
  47. #define    OREQ    297
  48. #define    OROR    298
  49. #define    POINTER    299
  50. #define    REGISTER    300
  51. #define    RETURN    301
  52. #define    RSHIFT    302
  53. #define    RSHIFTEQ    303
  54. #define    SCON    304
  55. #define    SHORT    305
  56. #define    SIGNED    306
  57. #define    SIZEOF    307
  58. #define    STATIC    308
  59. #define    STRUCT    309
  60. #define    SUBEQ    310
  61. #define    SWITCH    311
  62. #define    TYPEDEF    312
  63. #define    UNION    313
  64. #define    UNSIGNED    314
  65. #define    VOID    315
  66. #define    VOLATILE    316
  67. #define    WHILE    317
  68. #define    XOREQ    318
  69. #define    EOI    319
  70.  
  71. typedef unsigned int unint;
  72. typedef unsigned char uchar;
  73.  
  74. #define    YYCTYPE        uchar
  75. #define    YYCURSOR    cursor
  76. #define    YYLIMIT        s->lim
  77. #define    YYMARKER    s->ptr
  78. #define    YYFILL(n)    {cursor = fill(s, cursor);}
  79.  
  80. #define    RET(i)    {s->cur = cursor; return i;}
  81.  
  82. typedef struct Scanner {
  83.     uchar        *tok, *ptr, *cur, *pos, *lim, *eof;
  84.     unint        line;
  85. } Scanner;
  86.  
  87. uchar *fill(Scanner *s, uchar *cursor){
  88.     if(!s->eof){
  89.     unint cnt = s->lim - s->tok;
  90.     uchar *buf = malloc((cnt + 1)*sizeof(uchar));
  91.     memcpy(buf, s->tok, cnt);
  92.     cursor = &buf[cursor - s->tok];
  93.     s->pos = &buf[s->pos - s->tok];
  94.     s->ptr = &buf[s->ptr - s->tok];
  95.     s->lim = &buf[cnt];
  96.     s->eof = s->lim; *(s->eof)++ = '\n';
  97.     s->tok = buf;
  98.     }
  99.     return cursor;
  100. }
  101.  
  102. int scan(Scanner *s){
  103.     uchar *cursor = s->cur;
  104. std:
  105.     s->tok = cursor;
  106. /*!re2c
  107. any    = [\000-\377];
  108. O    = [0-7];
  109. D    = [0-9];
  110. L    = [a-zA-Z_];
  111. H    = [a-fA-F0-9];
  112. E    = [Ee] [+-]? D+;
  113. FS    = [fFlL];
  114. IS    = [uUlL]*;
  115. ESC    = [\\] ([abfnrtv?'"\\] | "x" H+ | O+);
  116. */
  117.  
  118. /*!re2c
  119.     "/*"            { goto comment; }
  120.     
  121.     "auto"            { RET(AUTO); }
  122.     "break"            { RET(BREAK); }
  123.     "case"            { RET(CASE); }
  124.     "char"            { RET(CHAR); }
  125.     "const"            { RET(CONST); }
  126.     "continue"        { RET(CONTINUE); }
  127.     "default"        { RET(DEFAULT); }
  128.     "do"            { RET(DO); }
  129.     "double"        { RET(DOUBLE); }
  130.     "else"            { RET(ELSE); }
  131.     "enum"            { RET(ENUM); }
  132.     "extern"        { RET(EXTERN); }
  133.     "float"            { RET(FLOAT); }
  134.     "for"            { RET(FOR); }
  135.     "goto"            { RET(GOTO); }
  136.     "if"            { RET(IF); }
  137.     "int"            { RET(INT); }
  138.     "long"            { RET(LONG); }
  139.     "register"        { RET(REGISTER); }
  140.     "return"        { RET(RETURN); }
  141.     "short"            { RET(SHORT); }
  142.     "signed"        { RET(SIGNED); }
  143.     "sizeof"        { RET(SIZEOF); }
  144.     "static"        { RET(STATIC); }
  145.     "struct"        { RET(STRUCT); }
  146.     "switch"        { RET(SWITCH); }
  147.     "typedef"        { RET(TYPEDEF); }
  148.     "union"            { RET(UNION); }
  149.     "unsigned"        { RET(UNSIGNED); }
  150.     "void"            { RET(VOID); }
  151.     "volatile"        { RET(VOLATILE); }
  152.     "while"            { RET(WHILE); }
  153.     
  154.     L (L|D)*        { RET(ID); }
  155.     
  156.     ("0" [xX] H+ IS?) | ("0" D+ IS?) | (D+ IS?) |
  157.     (['] (ESC|any\[\n\\'])* ['])
  158.                 { RET(ICON); }
  159.     
  160.     (D+ E FS?) | (D* "." D+ E? FS?) | (D+ "." D* E? FS?)
  161.                 { RET(FCON); }
  162.     
  163.     (["] (ESC|any\[\n\\"])* ["])
  164.                 { RET(SCON); }
  165.     
  166.     "..."                   { RET(ELLIPSIS); }
  167.     ">>="            { RET(RSHIFTEQ); }
  168.     "<<="            { RET(LSHIFTEQ); }
  169.     "+="            { RET(ADDEQ); }
  170.     "-="            { RET(SUBEQ); }
  171.     "*="            { RET(MULEQ); }
  172.     "/="            { RET(DIVEQ); }
  173.     "%="            { RET(MODEQ); }
  174.     "&="            { RET(ANDEQ); }
  175.     "^="            { RET(XOREQ); }
  176.     "|="            { RET(OREQ); }
  177.     ">>"            { RET(RSHIFT); }
  178.     "<<"            { RET(LSHIFT); }
  179.     "++"            { RET(INCR); }
  180.     "--"            { RET(DECR); }
  181.     "->"            { RET(DEREF); }
  182.     "&&"            { RET(ANDAND); }
  183.     "||"            { RET(OROR); }
  184.     "<="            { RET(LEQ); }
  185.     ">="            { RET(GEQ); }
  186.     "=="            { RET(EQL); }
  187.     "!="            { RET(NEQ); }
  188.     ";"            { RET(';'); }
  189.     "{"            { RET('{'); }
  190.     "}"            { RET('}'); }
  191.     ","            { RET(','); }
  192.     ":"            { RET(':'); }
  193.     "="            { RET('='); }
  194.     "("            { RET('('); }
  195.     ")"            { RET(')'); }
  196.     "["            { RET('['); }
  197.     "]"            { RET(']'); }
  198.     "."            { RET('.'); }
  199.     "&"            { RET('&'); }
  200.     "!"            { RET('!'); }
  201.     "~"            { RET('~'); }
  202.     "-"            { RET('-'); }
  203.     "+"            { RET('+'); }
  204.     "*"            { RET('*'); }
  205.     "/"            { RET('/'); }
  206.     "%"            { RET('%'); }
  207.     "<"            { RET('<'); }
  208.     ">"            { RET('>'); }
  209.     "^"            { RET('^'); }
  210.     "|"            { RET('|'); }
  211.     "?"            { RET('?'); }
  212.  
  213.  
  214.     [ \t\v\f]+        { goto std; }
  215.  
  216.     "\n"
  217.         {
  218.         if(cursor == s->eof) RET(EOI);
  219.         s->pos = cursor; s->line++;
  220.         goto std;
  221.         }
  222.  
  223.     any
  224.         {
  225.         printf("unexpected character: %c\n", *s->tok);
  226.         goto std;
  227.         }
  228. */
  229.  
  230. comment:
  231. /*!re2c
  232.     "*/"            { goto std; }
  233.     "\n"
  234.         {
  235.         if(cursor == s->eof) RET(EOI);
  236.         s->tok = s->pos = cursor; s->line++;
  237.         goto comment;
  238.         }
  239.         any            { goto comment; }
  240. */
  241. }
  242.  
  243. #ifndef    MAP_NORESERVE
  244. #define    MAP_NORESERVE    0
  245. #endif
  246.  
  247. main(){
  248.     Scanner in;
  249.     struct stat statbuf;
  250.     uchar *buf;
  251.     fstat(0, &statbuf);
  252.     buf = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED|MAP_NORESERVE,
  253.     0, 0);
  254.     if(buf != (uchar*)(-1)){
  255.     int t;
  256.     in.lim = &(in.cur = buf)[statbuf.st_size];
  257.     in.pos = NULL;
  258.     in.eof = NULL;
  259.     while((t = scan(&in)) != EOI){
  260. /*
  261.         printf("%d\t%.*s\n", t, in.cur - in.tok, in.tok);
  262.         printf("%d\n", t);
  263. */
  264.     }
  265.     munmap(buf, statbuf.st_size);
  266.     }
  267. }
  268.